<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2017 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: Dean <zxxjjforever@163.com>
// +----------------------------------------------------------------------
namespace api\home\controller;

use cmf\controller\RestUserBaseController;
class IndexController extends RestUserBaseController
{
    // api 首页
    public function index()
    {
        $this->userId;
        $data = [];
        $data['coin'] =  $this->user['coin'];
        $data['user_nickname'] =  $this->user['user_nickname'];
        $data['last_login_ip'] =  $this->user['last_login_ip'];
        $data['last_login_time'] =  date('Y-m-d H:i:s',$this->user['last_login_time']);
        $this->success('ok',$data);
    }
    
    private function trimData($rs){
        if($rs && ($rs[0]=='{'||$rs[0]=='[')){
            $data = json_decode($rs,true);
            if(isset($data['TaskName'])){
                unset($data['TaskName']);
            }
            if(isset($data['系统信息'])){
                $roomRege = '/房间ID\W+(\d+)/';
                $mt = [];
                preg_match($roomRege, $data['系统信息'], $mt);
                if(count($mt)>1){
                    $data['roomId'] = $mt[1];
                }
                unset($data['系统信息']);
            }
            return $data;
        }
        return false;
    }
    
    public function read($id){
        $this->useCoin();
        if($id<10000||$id>999999){
            $this->error('房间不存在');
        }
        $url = $this->user['user_url'].'&act=gamedump&roomid='.$id;
        import('yxixia.NetUtil',EXTEND_PATH);
        $rs = \yxixia\NetUtil::getCurl($url);
        $data = $this->trimData($rs);
        if($data){
            $this->success('ok-rd',$data);
        }else{
            $this->error('找不到房间',$url);
        }
    }
    
    public function update($id){
        $this->useCoin();
        $args= $this->request->param('args');
        $url = $this->user['user_url'].'&act=gameop&roomid='.$id.'&args='.$args;
        import('yxixia.NetUtil',EXTEND_PATH);
        $rs = \yxixia\NetUtil::getCurl($url);
        $data = $this->trimData($rs);
        if($data){
            $this->success('ok-up',$data);
        }else{
            $this->error('找不到房间');
        }
        $this->success('ok');
    }    
}
